package com.t.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.t.blog.service.SysUserService;
import com.t.blog.utils.PassUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.t.pro.dao.SysUserDao;
import org.t.pro.pojo.SysRegisterUserPojo;
import com.t.blog.dto.SysUserDto;
import org.t.pro.pojo.SysUserInfoPojo;
import org.t.pro.vo.SysUser;

/**
* @author a0000
* @description 针对表【sys_user(用户信息表)】的数据库操作Service实现
* @createDate 2023-10-29 18:08:47
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser>
    implements UserDetailsService, SysUserService {

    @Autowired
    private PassUtils passUtils;

    @Autowired
    private SysUserDao mapper;



    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return getUserByUsername(username);
    }


    @Override
    public SysUserDto getUserByUsername(String loginWay, String username) {
        SysUserDto userDto = new SysUserDto();
        SysUser sysUserInfo = mapper.getSysUserInfo(loginWay, username);
        BeanUtils.copyProperties(sysUserInfo,userDto);
        return userDto;
    }

    @Override
    public SysUserDto getUserByUsername(String username) {
        SysUserDto userDto = new SysUserDto();
        SysUser sysUserInfo = mapper.getSysUserInfo("username", username);
        BeanUtils.copyProperties(sysUserInfo,userDto);
        return userDto;
    }

    @Override
    public SysUserDto getUserSalt(String username) {
        SysUserDto userDto = new SysUserDto();
        QueryWrapper<SysUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_name",username);
        userQueryWrapper.select("salt","user_name","nick_name");
        SysUser one = this.getOne(userQueryWrapper);
        BeanUtils.copyProperties(one,userDto);
        return userDto;
    }



    @Override
    public boolean saveUser(SysRegisterUserPojo sysUserDto) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDto,sysUser);
        //密码加盐加密
        SysUser passUser = passUtils.PassWithSalt(sysUser);
        passUser.setUserType("00");
        //保存数据库
        return this.save(passUser);
    }

    @Override
    public SysUserInfoPojo getUserById() {

        return null;
    }
}




